package ebj.common.java8.collection;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

public class MyCustomSet<T> extends HashSet<T> {
    /**
     * 求并集
     * return union of two collections
     * @param collection1
     * @param collection2
     * @return
     */
    public <T> Collection<T> union(Collection<T> collection1, Collection<T> collection2) {
        Set<T> set = new HashSet<T>(collection1);
        set.addAll(collection2);
        return set;
    }
     
    /**
     * 求交集
     * return intersection of two collections
     * @param collection1
     * @param collection2
     * @return
     */
    public <T> Collection<T> intersection(Collection<T> collection1, Collection<T> collection2) {
        Set<T> set = new HashSet<T>(collection1);
        set.retainAll(collection2);
        return set;
    }
     
    /**
     * 求差集
     * return set difference of two collections
     * @param collection1
     * @param collection2
     * @return
     */
    public <T> Collection<T> setDifference(Collection<T> collection1, Collection<T> collection2) {
        Set<T> set = new HashSet<T>(collection1);
        set.removeAll(collection2);
        return set;
    }
}